package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.modules.hos.entity.MedicineTypeEntity;
import io.renren.modules.hos.service.MedicineTypeService;
import io.renren.modules.hos.vo.MedicineVo;
import io.renren.modules.hos.vo.params.SearchParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.MedicineDao;
import io.renren.modules.hos.entity.MedicineEntity;
import io.renren.modules.hos.service.MedicineService;


@Service("medicineService")
public class MedicineServiceImpl extends ServiceImpl<MedicineDao, MedicineEntity> implements MedicineService {

    @Autowired
    private MedicineTypeService medicineTypeService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MedicineEntity> page = this.page(
                new Query<MedicineEntity>().getPage(params),
                new QueryWrapper<MedicineEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String selectMedicineType(Integer medicineType) {
        MedicineTypeEntity medicineTypeEntity = medicineTypeService.selectMedicineTypeByType(medicineType);
        return medicineTypeEntity.getTypeName();
    }

    @Override
    public PageUtils selectMedicineList(Map<String, Object> params) {
        LambdaQueryWrapper<MedicineEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<MedicineEntity> medicineEntities = baseMapper.selectList(queryWrapper);
        int pageSize = (int) params.get("pageSize");
        int pageIndex = (int) params.get("pageIndex");
        List<MedicineVo> medicineVos = copyList(medicineEntities);

        return new PageUtils(medicineVos, medicineVos.size(), pageSize, pageIndex);
    }

    @Override
    public PageUtils search(SearchParam searchParam) {
        String keyword = searchParam.getKeyword();
        System.out.println(keyword);
        List<MedicineVo> search = baseMapper.search(keyword);
        return new PageUtils(search, search.size(), searchParam.getPageSize(), searchParam.getPageIndex());
    }

    public MedicineVo copy(MedicineEntity medicineEntity){
        MedicineVo medicineVo = new MedicineVo();
        BeanUtils.copyProperties(medicineEntity, medicineVo);
        if (medicineEntity.getMedicineNumber() != 0){
            medicineVo.setStockStatus(0);
        }else {
            medicineVo.setStockStatus(1);
        }

        return medicineVo;
    }

    public List<MedicineVo> copyList(List<MedicineEntity> medicineEntities){
        List<MedicineVo> medicineVos = new ArrayList<>();
        for (MedicineEntity medicineEntity : medicineEntities) {
            medicineVos.add(copy(medicineEntity));
        }

        return medicineVos;
    }
}